<?php


/*
 * Created on 14/12/2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

function echoTeamRow(& $team, & $teamStats, $row) {
	global $sd, $loggedin, $rootDir;
	$canEdit = isset ($loggedin) and $loggedin->canUpdateTeam($team); 
	
	$wonSeasons = $sd->findWonSeasons($team->id);
    echo getEchoTR($row, "center");
    echo "  <td>" . $team->getRosterLink() . "</td>\n";
    echo "  <td><a href=\"race_viewer.php?race_id=" . $team->race->id . "\">" . getEchoValue($team->race->name) . "</a></td>\n";
    echo "  <td>" . getEchoValue($team->getTR()) . "</td>\n";
    echo "  <td>" . getEchoValue($teamStats->played) . "</td>\n";
    echo "  <td>" . getEchoValue($teamStats->won) . "</td>\n";
    echo "  <td>" . getEchoValue($teamStats->drawn) . "</td>\n";
    echo "  <td>" . getEchoValue($teamStats->lost) . "</td>\n";
    echo "  <td>" . getEchoValue($teamStats->getNetTDs()) . "</td>\n";
    echo "  <td>" . getEchoValue($teamStats->getNetCas()) . "</td>\n";
    if ($teamStats->played > 0) {
        $record = ($teamStats->won + ($teamStats->drawn / 2)) / $teamStats->played;
    }
    $record = sprintf("%1.3f", $record);
    echo "  <td>" . getEchoValue($record) . "</td>\n";
    echo "  <td><font size=\"-2\">" . getEchoValue($team->lastUpdate) . "</font></td>\n<td align=\"center\">";
	foreach($wonSeasons as $wonSeason) {
	    if (strlen($wonSeason->image) > 0) {
echo<<< EOQ
		<img src="$wonSeason->image" TITLE="Winner $wonSeason->season :- $wonSeason->imageText" BORDER="0">
EOQ;
	    } else {
echo<<< EOQ
		<div class="sup"><span title="Winner $wonSeason->season :- $wonSeason->imageText">Winner<br />$wonSeason->season</span</div>
EOQ;
	    } 
	}
	if ($canEdit) {				
    echo<<< EOQ
		<a href="team_editor.php?id=$team->id"><img src="$rootDir/images/edit.jpg" title="Edit Team"/></a>
EOQ;
	}
    echo<<< EOQ
		<a href="pf_team_viewer.php?id=$team->id" target="dsl_pf"><img src="$rootDir/images/printer.gif" title="Printer Friendly" /></a></td>
EOQ;
}

/*
 * Controller
 */
include_once ("includes/init.php");
$id = getParam("id");
if (!isset ($id)) {
    $redirectStr = "No such coach";
    $message = "Unable to identify coach to show";
    include ("includes/redirect.php");
    return;
}

$coach = loadCoach($id);
if (!$coach) {
    $redirectStr = "No such coach";
    $message = "Unable to identify coach to show";
    include ("includes/redirect.php");
    return;
}

$showStats = false;
$title = "Coach " . $coach->name;
include_once ("includes/header.php");
echo<<< EOQ
    <caption><h1>$coach->name</h1></caption>
EOQ;

/*
 * Match Approvals
 */
if (isset ($loggedin) && $loggedin->id==$id) {
echo<<< EOQ
	<tr><td align="right"><a href="login.php?out=true">Log out</a></td><td></td></tr>
	<tr><td align="center"><h4>Match Approvals</h4></td></tr>
	<tr>
		<td colspan="2">
		<table border="0" cellpadding="4" cellspacing="0" width="100%">
EOQ;
$ad = new ApprovalDao();
$myApprovals = $ad->findOutstandingCoachApprovals();
$commApprovals = $ad->findOutstandingCommissionerApprovals();
if (count($myApprovals) == 0) {
	echo<<< EOQ
		<tr><td>No outstanding approvals</td></tr>
EOQ;
} else {
	foreach ($myApprovals as $approval) {
		$match = $matches->getMatch($approval->matchId);
		$desc = $match->getDescription();
	echo<<< EOQ
		<tr><td><a href="match_view.php?matchId=$approval->matchId">$desc</a></td></tr>
EOQ;
	}
}
if (count($commApprovals) > 0) {
	echo<<< EOQ
		<tr><th>Commissioner approvals</th></tr>
EOQ;
	foreach ($commApprovals as $approval) {
		$match = $matches->getMatch($approval->matchId);
		$desc = $match->getDescription();
echo<<< EOQ
		<tr><td><a href="match_view.php?matchId=$approval->matchId">$desc</a></td></tr>
EOQ;
	}
}
echo<<< EOQ
	</table></td></tr>
EOQ;
}
/*
 * Achievements
 */
$cd = new CoachDao();
$achievementSeasonIds = $cd->findPlayOffAppearances($coach->id);
if (count($achievementSeasonIds) > 0) {
echo<<< EOQ
	<tr><td align="center"><h4>Achievements</h4></td></tr>
	<tr><td><h5>
EOQ;
	$keys = array_keys($achievementSeasonIds);
	foreach($keys as $key) {
	    $targetSeason = new Season($key);
	    $result = $achievementSeasonIds[$key];
	    if (!strcmp("Final", $result)) {
	    	$winningTeam = $targetSeason->getWinner($teams);
	        if ($winningTeam->coach->id == $coach->id) {
	            $result = "Winner";
	        } else {
	            $result = "Finalist";
	        }
	    } else {
	        $result = "Semi-finalist";
	    }
echo<<< EOQ
    $result $targetSeason->season<br />
EOQ;
	}
echo<<< EOQ
    </h5></td></tr>
EOQ;
	
} 

/*
 * Team Performances 
 */
echo<<< EOQ
	<tr><td align="center"><h4>Teams</h4></td></tr>
    <tr><td colspan="2"><table border="0" cellspacing="0" cellpadding="1" width="100%">
    <tr>
    	<th>Team Name</th>
    	<th>Race</th>
    	<th>TV</th>
    	<th>Pld</th>
    	<th>W</th>
    	<th>D</th>
    	<th>L</th>
    	<th>Net<br/>TDs</th>
    	<th>Net<br/>Cas</th>
    	<th>Record</th>
    	<th>Last<br />Update</th>
    	<th>&nbsp;</th>
	</tr>
EOQ;
$teamDao = new TeamDao();
$coachTeams = $teamDao->findCoachTeams($coach);
$row = 0;
$coachStats = new TeamStats();
foreach ($coachTeams as $team) {
	$teamStats = $team->getStats();
    echoTeamRow($team, $teamStats, $row++);
    flush(); ob_flush();
    $coachStats->played += $teamStats->played;
    $coachStats->won += $teamStats->won;
    $coachStats->drawn += $teamStats->drawn;
    $coachStats->lost += $teamStats->lost;
    $coachStats->tds += $teamStats->getNetTDs();
    $coachStats->cas += $teamStats->getNetCas();
}
if ($coachStats->played > 0) {
    $record = ($coachStats->won + ($coachStats->drawn / 2)) / $coachStats->played;
}
$record = sprintf("%1.3f", $record);
echo<<< EOQ
    <tr align="center">
    	<th colspan="3">Overall</th>
    	<td>$coachStats->played</td>
    	<td>$coachStats->won</td>
    	<td>$coachStats->drawn</td>
    	<td>$coachStats->lost</td>
    	<td>$coachStats->tds</td>
    	<td>$coachStats->cas</td>
    	<td>$record</td>
    	<td>&nbsp;</td>
	</tr>
    </table></td></tr>
EOQ;

/*
 * Email 
 */
if ($coach->hasEmail() && isset ($loggedin) && $loggedin->id != $id) {
    $submit = getParam("submit");
    $subject = stripslashes(getParam("subject"));
    $comment = stripslashes(getParam("comment"));
    echo<<< EOQ
    <tr><th colspan="2"><h2>Send $coach->name a message</h2></th></tr>
	<form method="post">
    <tr>
    	<td>Subject:</td>
    	<td><input type="text" size="64" maxsize="128" name="subject" value="$subject"></td>
	</tr>
    <tr>
    	<td colspan="2"><TEXTAREA name="comment" rows="10" cols="100">$comment</TEXTAREA></td>
	</tr>
    <tr>
    	<td colspan="2" align="center"><input type="submit" name="submit" value="Send Message"></td>
	</tr>
	</form>
	<form>
    <tr>
    	<td colspan="2" align="center"><input type="submit" name="clear" value="Clear Form"></td>
	</tr>
	</form>
EOQ;
    if (isset ($submit) && strlen($comment) > 0 && strlen($subject) > 0) {
        $message = "$loggedin->name has send you this message through the $leagueNameShort website\n\n" . "***************************************************************************\n\n" . "From: $loggedin->name\n" . "Subject: $subject\n" . "Message:\n" . "$comment";

        $headers = "From: $loggedin->email\r\n" .
        "Reply-To: $loggedin->email\r\n" .
        "X-Mailer: PHP/" . phpversion();

        mail($coach->email, $subject, $message, $headers);
        echo<<< EOQ
    <tr>
    	<th colspan="2" align="center">Message sent</th>
	</tr>
EOQ;
    }
}

include_once ("includes/footer.php");
?>
